Skip to content

[ENHANCEMENT] Add Wavelet Tree Data Structure#7414

Merged
DenizAltunkapan merged 6 commits into
TheAlgorithms:masterfrom
space0032:add-wavelet-tree
May 15, 2026
Merged

[ENHANCEMENT] Add Wavelet Tree Data Structure#7414
DenizAltunkapan merged 6 commits into
TheAlgorithms:masterfrom
space0032:add-wavelet-tree

Conversation

@space0032
Copy link
Copy Markdown
Contributor

Description

This Pull Request introduces the Wavelet Tree data structure to the datastructures/trees package in the repository. A Wavelet Tree is a highly efficient and versatile data structure used for compressed storage of sequences and answering complex range-based queries in O(log U) time.

The main operations implemented are:

  1. rank(x, i): Determines how many times the value x appears from the beginning of the array up to the index i.
  2. kthSmallest(l, r, k): Finds the k-th smallest element in the subarray [l, r].

The functionality is generalized to handle large input arrays with an arbitrary range of values (minValue and maxValue).

Files Added

  1. WaveletTree.java: Contains the implementation of the Wavelet Tree data structure.
  2. WaveletTreeTest.java: Contains JUnit test cases to validate the correctness of the methods and handle edge cases.

Complexity

  • Time Complexity:
    • Tree Construction: $O(N \log U)$, where $N$ is the length of the array and $U$ is the range of the values.
    • Query Operations: $O(\log U)$ per query.
  • Space Complexity:
    • $O(N \log U)$ for storing the bit vectors.

Copilot AI review requested due to automatic review settings May 6, 2026 09:48
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 6, 2026

Codecov Report

❌ Patch coverage is 94.28571% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.63%. Comparing base (e7f8979) to head (efe649a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...healgorithms/datastructures/trees/WaveletTree.java 94.28% 0 Missing and 6 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7414      +/-   ##
============================================
+ Coverage     79.56%   79.63%   +0.06%     
- Complexity     7197     7239      +42     
============================================
  Files           799      800       +1     
  Lines         23501    23606     +105     
  Branches       4624     4646      +22     
============================================
+ Hits          18699    18798      +99     
- Misses         4054     4055       +1     
- Partials        748      753       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new WaveletTree data structure under com.thealgorithms.datastructures.trees, providing rank, select, and kthSmallest (quantile) queries over an integer sequence, along with JUnit tests validating core behavior.

Changes:

  • Introduces WaveletTree implementation with rank(x, i), select(x, k), and kthSmallest(l, r, k).
  • Adds JUnit test suite covering typical cases, invalid inputs, empty/singleton arrays, and negative values.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/main/java/com/thealgorithms/datastructures/trees/WaveletTree.java Adds the Wavelet Tree implementation and query methods.
src/test/java/com/thealgorithms/datastructures/trees/WaveletTreeTest.java Adds JUnit tests for rank/select/kthSmallest, including some edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main/java/com/thealgorithms/datastructures/trees/WaveletTree.java Outdated
@space0032
Copy link
Copy Markdown
Contributor Author

The merged file is not showing up in the repo

@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) May 15, 2026 08:57
@DenizAltunkapan DenizAltunkapan merged commit 0811cd0 into TheAlgorithms:master May 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants